package com.hls.zfbai.core.aspect;

import com.alibaba.fastjson.JSONObject;
import com.hls.zfbai.core.entity.ResponseEntity;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class LogAspect4Rb {

    public LogAspect4Rb(){}

    @Pointcut(value = "execution(public * com.hls.zfbai.modules.*.web.*.*(..))")
    public void pointcut(){ }

    @Around(value = "pointcut()")
    public Object aroundLog(@NotNull ProceedingJoinPoint pjp) {
        String methodName = pjp.getSignature().getName(); // 获取执行的方法名称
        Object[] args = pjp.getArgs(); // 获取方法参数

        log.info("=========================================================================================");
        log.info("--"+ methodName +"-- ==> parameter：" + JSONObject.toJSONString(args));
        for (Object arg : args){
            log.info("--"+ methodName +"-- ==> parameter：" + JSONObject.toJSONString(arg));
        }

        // 定义返回参数
        Object result = null;
        try {
            result = pjp.proceed();
        } catch (Throwable throwable) {
            log.error(throwable.getMessage(), throwable);
            result = ResponseEntity.error("系统异常：" + throwable.getMessage() );
        }
        log.info("--"+ methodName +"-- <== return：" + JSONObject.toJSONString(result));
        log.info("=========================================================================================");
        return result;
    }

}
